package UI;

import Logic.*;

import UI.BaseModels.BCBSPanel;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;

/**
 * Class description of mainframe
 * 
 * 
 */
public class MainFrame extends JFrame {

	/**
	 *
	 */
	private static final long serialVersionUID = 5853369217732994530L;

	public static Account currentUser;
	public static BCBSPanel currentPanel;
	public static JPanel contentPane;

	// PANELS

	public static MainMenu main_menu;
	public static MainMenuAdmin admin_panel;

	public static LoginPanel login_start;
	public static TransactionsPanel transactions;
	public static PurchasePanel purchase_BCBS;
	public static SellPanel sell_BCBS;
	public static TransferPanel transfer_BCBS;
	public static CreateUserPanel create_User;
	public static EditUserSelfPanel edit_self;
	public static EditUserPanel edit_user;
	public static ListUsersPanel list_users;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		new MainFrame();
	}

	public MainFrame() {

		setResizable(false);

		setTitle("CBS BitCoin System");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 320, 480);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));

		login_start = new LoginPanel();
		contentPane.add(login_start, "name_1411060945626737");
		login_start.setVisible(true);

		purchase_BCBS = new PurchasePanel();
		contentPane.add(purchase_BCBS, "name_1413784361668497");
		purchase_BCBS.setVisible(false);

		main_menu = new MainMenu();
		contentPane.add(main_menu, "name_1411084773132641");
		main_menu.setVisible(false);

		transactions = new TransactionsPanel();
		contentPane.add(transactions, "name_16703336143349");
		transactions.setVisible(false);

		sell_BCBS = new SellPanel();
		contentPane.add(sell_BCBS, "name_176560504671051");
		sell_BCBS.setVisible(false);

		transfer_BCBS = new TransferPanel();
		contentPane.add(transfer_BCBS, "name_187164259315912");
		transfer_BCBS.setVisible(false);

		create_User = new CreateUserPanel();
		contentPane.add(create_User, "name_187164259315962");
		create_User.setVisible(false);

		admin_panel = new MainMenuAdmin();
		contentPane.add(admin_panel, "name_182164259315912");
		admin_panel.setVisible(false);

		edit_user = new EditUserPanel();
		contentPane.add(edit_user, "name_187164254315912");
		edit_user.setVisible(false);

		edit_self = new EditUserSelfPanel();
		contentPane.add(edit_self, "name_187164554215112");
		edit_self.setVisible(false);

		list_users = new ListUsersPanel();
		contentPane.add(list_users, "name_127166554245912");
		list_users.setVisible(false);

		setVisible(true);
	}

	public static class LogOut implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			logout();

		}
	}

	/**
	 * Resets the interface logic for all fields, sets the current user to null,
	 * hides the current panel and displays the login-panel.
	 */
	private static void logout() {

		// Reset interface logic
		currentUser = null;
		currentPanel.stopThread();
		login_start.setVisible(true);
		login_start.setEnabled(true);
		currentPanel.setVisible(false);
		currentPanel.setEnabled(false);
		currentPanel = login_start;

		// clear interface fields
		create_User.clearFields();
		edit_user.clearFields();
		login_start.clearFields();
		purchase_BCBS.clearFields();
		sell_BCBS.clearFields();
		transactions.clearFields();
		transfer_BCBS.clearFields();
		edit_self.clearFields();
		main_menu.clearFeedback();
		list_users.clearFields();
	}

	/**
	 * Mainframe panel switch method. Clears and hides the current panel and
	 * switches to selected panel. Tells the current panel to terminate its
	 * current thread. Calls required methods on the panel supplied as parameter
	 * based on the type of panel. An Account object may be attached for the
	 * edit_user panel to populate its fields with info based on this account.
	 * 
	 * @param panel
	 *            The panel to switch to.
	 * @param selectedUser
	 *            Optionally attached account object. Supply null if not
	 *            relevant.
	 */
	public static void switchToPanel(BCBSPanel panel, Account selectedUser) {

		// ask current panel to stop whatever it's doing
		currentPanel.stopThread();
		// clear fields for current panel
		currentPanel.clearFields();

		// show new panel, hide current
		panel.setVisible(true);
		panel.setEnabled(true);
		currentPanel.setVisible(false);
		currentPanel.setEnabled(false);

		// set current panel to new panel
		currentPanel = panel;

		if (panel == main_menu) {
			main_menu.refreshInfo();
		} else {
			main_menu.clearFeedbackTop();
		}

		if (panel == transactions) {
			transactions.refreshTransactions();

		} else if (panel == edit_self) {
			edit_self.getSelfInfo();
		} else

		if (panel == list_users) {
			list_users.refreshUsers();
		} else

		if (panel == edit_user && selectedUser != null) {
			edit_user.getUserInfo(selectedUser);
			selectedUser = null;
		}
	}
}
